package com.dvd.djc.gdjc002;
import java.io.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.dvd.common.CommonAction;
import com.dvd.dao.dvd.*;
import com.dvd.utils.*;
public class GDJC002Action extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonAction.New(request,this.servlet);
		String dvdId = request.getParameter("dvdId");
		String action= request.getParameter("action");
		if(action.equals("newentry") || action.equals("editentry"))
		{
			GDJC002Bean bean = (GDJC002Bean)form;
			
			//如果获取了文件，上传文件并且改变文件路径
			FormFile file = bean.getFImgFile();;
			String fileName = null;
			if(file!=null && (fileName=file.getFileName()).length()>0)
			{
				String ext = fileName.substring(fileName.lastIndexOf('.'),fileName.length());
				bean.setFImgPath("uploadimages/" +bean.getFDvdId() +ext);
				this.uploadFile(bean);
			}
			TDvd dvd = new TDvd(bean.getFDvdId(),bean.getFDvdTitle(),bean.getFYear(),bean.getFType(),
					bean.getFCountry(),bean.getFPrice(),bean.getFImgPath(),bean.getFDirector(),
					bean.getFActor(),bean.getFMemo(),new Date(bean.getFCrtDt()),new Date());
			return this.entryDvd(action,dvd, mapping, form, request, response);
		}
		
		
		/*to add DVD */
		else if(action.equals("new"))
		{
			TDvd dvd = new TDvd(("" + new Date().getTime()).substring(0,12));	
			request.setAttribute("TDvd", dvd);
			return new ActionForward("/GDJC002.jsp?stateFlag=newentry");
		}
		
		/*to edit DVD*/
		else if(action.equals("edit"))
		{
		
			GDJC002Model model = new GDJC002Model();
			TDvd dvd =null;
			
			
			dvd=model.searchDvdInfo(dvdId);
			System.out.println("aa");
			
			if(dvd==null)
			{
				return new ActionForward("/error.jsp?error=GDJC002ActionEditDvdNull");
			}	
			request.setAttribute("TDvd",dvd);
			return new ActionForward("/GDJC002.jsp?stateFlag=editentry");
		}
		return null;
	}

	/**
	 * @param flag "editentry" or "newentry"
	 * @param dvd
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping
	 * eidt DVD or new Dvd
	 */
	public ActionForward entryDvd(String flag,TDvd dvd,ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		
		GDJC002Model model = new GDJC002Model();
		boolean result=false;
		result=model.entryDvd(flag,dvd);
		List<Message> messages = new ArrayList<Message>();
		Message m = null;
		if (result == true)
		{
			if(flag.equals("newentry"))
			{
				return mapping.findForward("GDJC003");
			}
			else
			{
				m=new Message("","INFO","DVD情報成功に更新しました！");
			}
		}
		else
		{
			m=new Message("","INFO","重複なDVD情報は存在しました！");
		}
		messages.add(m);
		request.setAttribute("List<Message>", messages);
		request.setAttribute("TDvd",dvd);		
		return new ActionForward("/GDJC002.jsp?stateFlag=editentry");
	}
	

	/**
	 * @param bean GDJC002Bean
	 * @param fileName 
	 * @return 
	 */
	private boolean uploadFile(GDJC002Bean bean)
	{
		String filePath=bean.getFImgPath();
		InputStream is = null;
		OutputStream os = null;
		try {
			 is = bean.getFImgFile().getInputStream();
			 os = new FileOutputStream(servlet.getServletContext().getRealPath("/") +filePath);
			 int ch;
			 while((ch=is.read()) != -1)
			 {
				 os.write(ch);
			 }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally
		{
			try
			{
				is.close();
				os.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return true;
	}

}
